package com.kensure.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import co.kensure.frame.ResultInfo;
import co.kensure.frame.ResultRowInfo;
import co.kensure.http.RequestUtils;

import com.alibaba.fastjson.JSONObject;
import com.kensure.shike.baobei.model.SKKanJia;
import com.kensure.shike.baobei.model.SKKanJiaDetail;
import com.kensure.shike.baobei.service.SKKanJiaDetailService;
import com.kensure.shike.baobei.service.SKKanJiaService;
import com.kensure.shike.user.service.SKUserService;

/**
 * 砍价
 * 
 * @author fankaidi
 *
 */
@Controller
@RequestMapping(value = "kanjia")
public class KanJiaController {

	@Resource
	private SKUserService sKUserService;
	@Resource
	private SKKanJiaService sKKanJiaService;
	@Resource
	private SKKanJiaDetailService sKKanJiaDetailService;

	/**
	 * 根据商品id获取砍价情况
	 */
	@ResponseBody
	@RequestMapping(value = "getinfo.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getinfo(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long bbid = json.getLong("id");
		SKKanJia kanjia = sKKanJiaService.getKanJia(sKUserService.getUser().getId(), bbid);
		return new ResultRowInfo(kanjia);
	}
	
	/**
	 * 根据砍价id获取砍价情况
	 */
	@ResponseBody
	@RequestMapping(value = "getkanjia.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getkanjia(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long kjid = json.getLong("id");
		SKKanJia kanjia = sKKanJiaService.getFullOne(kjid);
		return new ResultRowInfo(kanjia);
	}
	

	/**
	 * 前端，提交砍价申请
	 */
	@ResponseBody
	@RequestMapping(value = "kanjia.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo kanjia(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long bbid = json.getLong("id");
		SKKanJia kanjia = sKKanJiaService.add(sKUserService.getUser().getId(), bbid);
		return new ResultRowInfo(kanjia);
	}

	/**
	 * 前端，参与砍价
	 */
	@ResponseBody
	@RequestMapping(value = "kanjiadet.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo kanjiadet(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long kjid = json.getLong("id");
		SKKanJiaDetail kanjiadet = sKKanJiaDetailService.add(kjid, sKUserService.getUser().getId());
		return new ResultRowInfo(kanjiadet);
	}
	
	/**
	 * 根据砍价id获取砍价详情
	 */
	@ResponseBody
	@RequestMapping(value = "getkanjiadet.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getkanjiadet(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		Long kjid = json.getLong("id");
		 SKKanJiaDetail kanjiadet = sKKanJiaDetailService.getKanJia(kjid,sKUserService.getUser().getId());
		return new ResultRowInfo(kanjiadet);
	}
	
}
